package string;

/**
 * @author Jisheng Huang
 * @version 20250604
 */
public class LastStringFromBoxI_3403_2 {
    public static String answerString(String word, int numFriends) {
        if (numFriends == 1) {
            return word;
        }

        int n = word.length();
        String res = "";

        for (int i = 0; i < n; ++i) {
            String s = word.substring(i, Math.min(i + n - numFriends + 1, n));

            if (res.compareTo(s) <= 0) {
                res = s;
            }
        }

        return res;
    }

    public static void main(String[] args) {
        String word = "dbca";
        int numFriends = 2;

        System.out.println(answerString(word, numFriends));

        word = "gggg";
        numFriends = 4;

        System.out.println(answerString(word, numFriends));
    }
}
